package com.graduation.manage.service.impl;

import java.time.Duration;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.graduation.common.utils.DateUtils;
import com.graduation.manage.domain.Role;
import com.graduation.manage.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.graduation.manage.mapper.TbUserMapper;
import com.graduation.manage.domain.TbUser;
import com.graduation.manage.service.ITbUserService;

/**
 * 用户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-12
 */
@Slf4j
@Service
public class TbUserServiceImpl implements ITbUserService 
{
    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IRoleService roleService;

    /**
     * 查询用户管理
     * 
     * @param userId 用户管理主键
     * @return 用户管理
     */
    @Override
    public TbUser selectTbUserByUserId(Long userId)
    {
        return tbUserMapper.selectTbUserByUserId(userId);
    }

    /**
     * 查询用户管理列表
     * 
     * @param tbUser 用户管理
     * @return 用户管理
     */
    @Override
    public List<TbUser> selectTbUserList(TbUser tbUser)
    {
        if(tbUser.getParams().get("isTch").equals("true")) {
            tbUser.setRoleId(1L);
        } else {
            tbUser.setRoleId(2L);
        }
        log.info("tbUser: " + tbUser);
        return tbUserMapper.selectTbUserList(tbUser);
    }

    /**
     * 新增用户管理
     * 
     * @param tbUser 用户管理
     * @return 结果
     */
    @Override
    public int insertTbUser(TbUser tbUser)
    {
        tbUser.setCreateTime(DateUtils.getNowDate());
        tbUser.setUpdateTime(DateUtils.getNowDate());
        Long roleId = tbUser.getRoleId();
        tbUser.setUserCode(generateUserCode(roleId));
        Role role = roleService.selectRoleByRoleId(roleId);
        tbUser.setRoleName(role.getRoleName());
        tbUser.setRoleCode(role.getRoleCode());
        return tbUserMapper.insertTbUser(tbUser);
    }

    /**
     * 生成编码
     * @param roleId
     * @return
     */
    private String generateUserCode(Long roleId) {
        String dateStr = DateUtils.getDate().replaceAll("-", "");
        String key;
        if(roleId == 1) {
            key  = "tch" + dateStr;
        } else {
            key = "stu" + dateStr;
        }
        if(!redisTemplate.hasKey(key)) {
            redisTemplate.opsForValue().set(key, 0, Duration.ofDays(1));
            return key + "0001";
        }
        redisTemplate.opsForValue().increment(key);
        return key + StrUtil.padPre(redisTemplate.opsForValue().get(key).toString(), 4, '0');
    }

    /**
     * 修改用户管理
     * 
     * @param tbUser 用户管理
     * @return 结果
     */
    @Override
    public int updateTbUser(TbUser tbUser)
    {
        tbUser.setUpdateTime(DateUtils.getNowDate());
        return tbUserMapper.updateTbUser(tbUser);
    }

    /**
     * 批量删除用户管理
     * 
     * @param userIds 需要删除的用户管理主键
     * @return 结果
     */
    @Override
    public int deleteTbUserByUserIds(Long[] userIds)
    {
        return tbUserMapper.deleteTbUserByUserIds(userIds);
    }

    /**
     * 删除用户管理信息
     * 
     * @param userId 用户管理主键
     * @return 结果
     */
    @Override
    public int deleteTbUserByUserId(Long userId)
    {
        return tbUserMapper.deleteTbUserByUserId(userId);
    }

    @Override
    public int assignTch(TbUser tbUser) {
        Long tchId = tbUser.getTchId();
        TbUser tch = tbUserMapper.selectTbUserByUserId(tchId);
        tbUser.setTchName(tch.getUserName());
        tbUser.setUpdateTime(DateUtils.getNowDate());
        return tbUserMapper.updateTbUser(tbUser);
    }
}
